package cn.gdcp.lkqapp18;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btnHello;
    private Button btn_star;
    private MyButton myBtn;
    private static final String TAG = "MainActivity";
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_star = findViewById(R.id.btn_start);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);
        btn4 = findViewById(R.id.btn4);
        btnHello = findViewById(R.id.btn_hello);
        myBtn = findViewById(R.id.btn_myBtn);

        btn_star.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //starAnimation();
                //starAnimation2();
                //starAnimation3();
                //starAnimatorByXML();
                starAnimatorOfObject();
            }
        });

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //starAnimation4();
                //starAnimation5();
                //starAnimation6();
                starAnimation7();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                float curTranslationX = btnHello.getTranslationX();
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btnHello,"translationX",
                        curTranslationX,600,curTranslationX);
                objectAnimator.setDuration(5000);
                objectAnimator.setRepeatCount(5);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        float value = (float) valueAnimator.getAnimatedValue();
                        btnHello.setTranslationY(value);
                        btnHello.setText("Hello" + value);
                    }
                });
                objectAnimator.start();
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btnHello,"scaleX",1,3,1);
                objectAnimator.setDuration(5000);
                objectAnimator.setRepeatCount(5);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.start();
            }
        });

        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btnHello,"rotation",0,360);
                objectAnimator.setDuration(5000);
                objectAnimator.setRepeatCount(5);
                objectAnimator.setRepeatMode(ValueAnimator.RESTART);
                objectAnimator.start();
            }
        });

    }

    private void starAnimation7() {
        // 步骤1：设置需要组合的动画效果
        float curTranslationX = myBtn.getTranslationX();
        ObjectAnimator translation = ObjectAnimator.ofFloat(myBtn, "translationX", curTranslationX, 400,curTranslationX);
        // 平移动画
        ObjectAnimator rotate = ObjectAnimator.ofFloat(myBtn, "rotation", 0f, 360f);
        // 旋转动画
        ObjectAnimator alpha = ObjectAnimator.ofFloat(myBtn, "alpha", 1f, 0f, 1f);
        // 透明度动画

        // 步骤2：创建组合动画的对象
        AnimatorSet animSet = new AnimatorSet();

        // 步骤3：根据需求组合动画
        animSet.play(translation).with(rotate).before(alpha);
        animSet.setDuration(5000);

        // 步骤4：启动动画
        animSet.start();
    }

    private void starAnimation6() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofInt(myBtn,"myText",1,1000,1);
        objectAnimator.setDuration(5000);
        objectAnimator.setRepeatCount(5);
        objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
        objectAnimator.start();
    }

    private void starAnimation5() {
        ObjectAnimator objectAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this,R.animator.set_animation2);
        objectAnimator.setTarget(btnHello);
        objectAnimator.setRepeatCount(5);
        objectAnimator.start();
    }

    private void starAnimation4() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btnHello,"alpha",1,0,1);
        objectAnimator.setDuration(5000);
        objectAnimator.setRepeatCount(5);
        objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
        objectAnimator.start();
    }

    private void starAnimatorOfObject() {
        Point pointStar = new Point(100,100);
        Point pointEnd = new Point(400,600);
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new PointEvaluator(),pointStar,pointEnd);
        valueAnimator.setDuration(5000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Point point = (Point) animation.getAnimatedValue();
                Log.e(TAG,"pointX:" + point.getX() + ",pointY:" + point.getY());
            }
        });
        valueAnimator.start();
    }

    private void starAnimatorByXML() {
        Animator animator = AnimatorInflater.loadAnimator(MainActivity.this,R.animator.set_animation);
        animator.setTarget(btn_star);
        ValueAnimator valueAnimator = (ValueAnimator) animator;
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                Log.e(TAG,"value = " + value);
                btn_star.setText("开始：宽高变化" + value);
                btn_star.getLayoutParams().width = value;
                btn_star.getLayoutParams().height = value;
                btn_star.requestLayout();
                btn_star.setBackgroundColor(Color.argb(255,value/2,164,204));
            }
        });
        valueAnimator.start();
    }

    private void starAnimation3() {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(btn_star.getLayoutParams().width,600);
        valueAnimator.setDuration(5000);
        valueAnimator.setStartDelay(2000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                Log.e(TAG,"value = " + value);
                btn_star.setText("开始：宽高变化" + value);
                btn_star.getLayoutParams().width = (int) value;
                btn_star.getLayoutParams().height = (int) value;
                btn_star.requestLayout();
            }
        });
        valueAnimator.start();
    }

    private void starAnimation2() {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(btn_star.getLayoutParams().width,600);
        valueAnimator.setDuration(5000);
        valueAnimator.setStartDelay(1000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                Log.e(TAG,"value = " + value);
                btn_star.setText("开始：宽度变化" + value);
                btn_star.getLayoutParams().width = value;
                btn_star.requestLayout();
            }
        });
        valueAnimator.start();
    }

    private void starAnimation() {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0,5);
        valueAnimator.setDuration(5000);
        valueAnimator.setStartDelay(1000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                Log.e(TAG,"value = " + value);
                btn_star.setText("开始：" + value);
                btn_star.requestLayout();
            }
        });
        valueAnimator.start();
    }
}
